/**
 * 
 */
package edu.sjtu.seda.stage;

import edu.sjtu.seda.event.EventHandler;
import edu.sjtu.seda.event.EventRejectHandler;
import edu.sjtu.seda.stage.api.StageConfig;

/**
 * @author john.zhu
 *
 */
public class StageImpl implements Stage {
	private String name;
	private StageWrapper stageWrapper;
	private EventHandler handler;
	private EventRejectHandler rejectHandler;
	
	public StageImpl(StageConfig config){
		this.name = config.getStage();
		if(config.getEventHandler()!=null||!config.getEventHandler().trim().equals("")){
			try {
				this.handler = (EventHandler) Class.forName(config.getEventHandler()).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		if(config.getEventRejectHandler()!=null&&!config.getEventRejectHandler().trim().equals("")){
//			try {
//				//not enough params to newInstance
//				this.rejectHandler = (EventRejectHandler) Class.forName(config.getEventRejectHandler()).newInstance();
//			} catch (InstantiationException e) {
//				e.printStackTrace();
//			} catch (IllegalAccessException e) {
//				e.printStackTrace();
//			} catch (ClassNotFoundException e) {
//				e.printStackTrace();
//			}
		}
		this.stageWrapper = new StageWrapperImpl(this,handler,rejectHandler,config);
	}

	/* (non-Javadoc)
	 * @see edu.sjtu.seda.stage.Stage#getName()
	 */
	@Override
	public String getName() {
		return this.name;
	}

	/* (non-Javadoc)
	 * @see edu.sjtu.seda.stage.Stage#getStageWrapper()
	 */
	@Override
	public StageWrapper getStageWrapper() {
		return this.stageWrapper;
	}

	@Override
	public void destroy() {
		this.stageWrapper.destroy();
	}

	@Override
	public void start() {
		this.stageWrapper.start();
	}

}
